/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.xy.controller;

import com.alibaba.excel.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xy.dto.VersionDTO;
import com.xy.dto.VersionRecordDTO;
import com.xy.entity.PositionGroupEntity;
import com.xy.entity.VenueEntity;
import com.xy.entity.VersionRecordEntity;
import com.xy.service.IPositionGroupService;
import com.xy.service.IVenueService;
import com.xy.service.IVersionRecordService;
import com.xy.vo.VersionRecordVO;
import com.xy.wrapper.VersionRecordWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xy.entity.VersionEntity;
import com.xy.vo.VersionVO;
import com.xy.wrapper.VersionWrapper;
import com.xy.service.IVersionService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 版本更新表 控制器
 *
 * @author BladeX
 * @since 2023-07-18
 */
@RestController
@AllArgsConstructor
@RequestMapping("version")
@Api(value = "版本更新表", tags = "版本更新表接口")
public class VersionController extends BladeController {

	private final IVersionService versionService;

	private final IVersionRecordService versionRecordService;

	private final IPositionGroupService positionGroupService;

	private final IVenueService venueService;

	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入version")
	public R<VersionVO> detail(VersionEntity version) {
		VersionEntity detail = versionService.getOne(Condition.getQueryWrapper(version));
		VersionVO versionVO = VersionWrapper.build().entityVO(detail);
		LambdaQueryWrapper<VersionRecordEntity> lambdaQueryWrapper = Wrappers.lambdaQuery();
		lambdaQueryWrapper.eq(VersionRecordEntity::getVersionId,detail.getId());
		List<VersionRecordEntity> entityList = versionRecordService.list(lambdaQueryWrapper);
		List<VersionRecordVO> listVO = VersionRecordWrapper.build().listVO(entityList);
		listVO.forEach(o->{
			PositionGroupEntity group = positionGroupService.getById(o.getPositionId());
			if(group != null ){
				o.setGroupName(group.getGroupName());
			}
			VenueEntity venue = venueService.getById(o.getVenueId());
			o.setVenueName(venue.getVenueName());
		});
		versionVO.setPositionGroupList(listVO);
		return R.data(versionVO);
	}
	/**
	 * 版本更新表 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入version")
	public R<IPage<VersionVO>> list(VersionEntity version, Query query) {
		IPage<VersionEntity> pages = versionService.page(Condition.getPage(query), Condition.getQueryWrapper(version));
		IPage<VersionVO> voiPage = VersionWrapper.build().pageVO(pages);
		List<VersionVO> entityList = voiPage.getRecords();
		entityList.forEach(o->{
			LambdaQueryWrapper<VersionRecordEntity> lambdaQueryWrapper = Wrappers.lambdaQuery();
			lambdaQueryWrapper.eq(VersionRecordEntity::getVersionId,o.getId());
			long count = versionRecordService.count(lambdaQueryWrapper);
			o.setUpdatePointSum(count);
		});
		voiPage.setRecords(entityList);
		return R.data(voiPage);
	}

	/**
	 * 版本更新表 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入version")
	public R<IPage<VersionVO>> page(VersionVO version, Query query) {
		IPage<VersionVO> pages = versionService.selectVersionPage(Condition.getPage(query), version);
		return R.data(pages);
	}

	/**
	 * 版本更新表 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入version")
	public R save(@Valid @RequestBody VersionDTO versionDTO) {
		VersionEntity versionEntity = Objects.requireNonNull(BeanUtil.copy(versionDTO, VersionEntity.class));
		return R.status(versionService.save(versionEntity));
	}

	/**
	 * 版本更新表 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入version")
	public R update(@Valid @RequestBody VersionEntity version) {
		return R.status(versionService.updateById(version));
	}

	/**
	 * 版本更新表 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入version")
	public R submit(@Valid @RequestBody VersionDTO versionDTO) {
		VersionEntity versionEntity = Objects.requireNonNull(BeanUtil.copy(versionDTO, VersionEntity.class));
		List<VersionRecordDTO> pointList = versionDTO.getPositionGroupList();
		boolean update = versionService.saveOrUpdate(versionEntity);
		if(CollectionUtils.isEmpty(pointList)){
			return R.status(update);
		}
		LambdaQueryWrapper<VersionRecordEntity> lambdaQueryWrapper = Wrappers.lambdaQuery();
		lambdaQueryWrapper.eq(VersionRecordEntity::getVersionId,versionEntity.getId());
		versionRecordService.remove(lambdaQueryWrapper);
		List<VersionRecordEntity> VersionRecordEntityList = new ArrayList<>();
		pointList.forEach(o->{
			VersionRecordEntity updateEntity = Objects.requireNonNull(BeanUtil.copy(o, VersionRecordEntity.class));
			updateEntity.setVersionId(versionEntity.getId());
			updateEntity.setReleaseType(versionEntity.getGrayscale());
			VersionRecordEntityList.add(updateEntity);
		});
		return R.status(versionRecordService.saveBatch(VersionRecordEntityList));
	}

	/**
	 * 版本更新表 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(versionService.deleteLogic(Func.toLongList(ids)));
	}

}
